﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;

namespace badminton.Pages
{
    public partial class HallSearchResult : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BadmintonEntities bde = new BadmintonEntities();
                this.DropDownListCityArea.Items.Add(new ListItem("不限", "-1"));
                foreach (CityArea ca in bde.CityArea)
                {
                    this.DropDownListCityArea.Items.Add(new ListItem(ca.name, ca.id.ToString()));
                }
                bindHallDropDown(-1);
                this.TextBoxStartTime.Text = DateTime.Now.ToString("yyyy.MM.dd hh:mm");

                if (Session["CourtSearch"] == null)
                {
                    return;
                }

                List<Hall> validHallIds = new List<Hall>();
                foreach (Hall hall in bde.Hall)
                {
                    if (isHallOK(hall))
                    {
                        validHallIds.Add(hall);
                   }
                }

                this.Repeater1.DataSource = validHallIds;// bde.Hall.Where(p => validHallIds.IndexOf(p.id) != -1);
                this.Repeater1.DataBind();     
            
            }


        }

        public string GetCityAreaName(object id)
        {
            int hallid = (int) id;
            BadmintonEntities bde = new BadmintonEntities();
            Hall hall = bde.Hall.FirstOrDefault(p => p.id == hallid);
            if (hall != null && hall.CityArea!=null)
            {
                return hall.CityArea.name;
            }

            return "";
        }

        protected bool isHallOK(Hall ct)
        {
            CourtSearchSession ts = Session["CourtSearch"] as CourtSearchSession;

            if (ts.m_hallId != -1)
            {
                if (ct.id != ts.m_hallId)
                {
                    return false;
                }
            }

            if (ts.m_cityAreaId != -1)
            {
                if (ct.cityAreaId != ts.m_cityAreaId)
                {
                    return false;
                }
            }




            return true;
        }

        protected bool isCourtOK(Court ct)
        {
            CourtSearchSession ts = Session["CourtSearch"] as CourtSearchSession;
            if (ts.m_startTime != null && ts.m_timeSpan > 0)
            {
                foreach (badminton.CourtSchedule cs in ct.CourtSchedule)
                {
                    if (cs.startTime != null && cs.endTime != null)
                    {
                        DateTime tsendtime = ts.m_startTime.AddHours(ts.m_timeSpan);
                        DateTime st = cs.startTime.Value > ts.m_startTime? ts.m_startTime:cs.startTime.Value;
                        DateTime et = cs.endTime.Value>tsendtime? cs.endTime.Value:tsendtime;

                        if ((et - st) < ((tsendtime-ts.m_startTime) + (cs.endTime.Value-cs.startTime.Value)))
                        {
                            return false;

                        }
                    }
                }

            }     
            
            return true;
        }

        public string GetCourtSchedule(object id)
        {
            int hallid = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            Hall hall = bde.Hall.FirstOrDefault(p => p.id == hallid);
            if (hall == null)
            {
                return "";
            }
            int unit = 18;
            CourtSearchSession courtSearch = Session["CourtSearch"] as CourtSearchSession;
            DateTime dt = courtSearch.m_startTime;

            DateTime dtstart = dt.Date;
            DateTime dtend = dtstart.AddDays(1);

            string ret = "<table border='0' cellspacing='0' cellpadding='0' width='100%'>";
            
            foreach (Court ct in hall.Court)
            {
                if (!isCourtOK(ct))
                {
                    continue;
                }
                ret = ret + "<tr><td>" + ct.name + "</td></tr><tr><td align='center'>";

                int courtId = ct.id;
                IQueryable<CourtSchedule> query = bde.CourtSchedule.Where(p => p.courtId == courtId && p.startTime > dtstart && p.startTime < dtend).OrderBy(p => p.startTime);
                List<CourtSchedule> sd = query.ToList();

                DateTime currtime = dtstart;
                foreach (CourtSchedule cs in sd)
                {
                    if (cs.startTime > currtime)
                    {
                        TimeSpan ts = cs.startTime.Value - currtime;
                        int len = Convert.ToInt32(ts.TotalHours * unit);
                        ret = ret + "<img src='../images/courtschedule/none.PNG' width='" + len.ToString() + "' height='20'>";
                    }

                    if (cs.endTime > cs.startTime)
                    {
                        string fn = "courtordered.PNG";
                        if (cs.CourtScheduleType.name == "培训")
                        {
                            fn = "courtintrain.PNG";
                        }
                        else if (cs.CourtScheduleType.name == "其他")
                        {
                            fn = "courtother.PNG";
                        }
                        TimeSpan ts = cs.endTime.Value - cs.startTime.Value;
                        int len = Convert.ToInt32(ts.TotalHours * unit);
                        ret = ret + "<img src='../images/courtschedule/" + fn + "' width='" + len.ToString() + "' height='20'>";

                    }

                    currtime = cs.endTime.Value;

                }

                ret = ret + "<img src='../images/courtschedule/none.PNG' width='" + Convert.ToInt32((dtend - currtime).TotalHours * unit).ToString() + "' height='20'>";

                ret = ret + "<hr /></td></tr>";

            }

            ret = ret + "</table>";


            return ret;
        }

        protected void OnSearchCourt(object sender, CommandEventArgs e)
        {
            DateTime dt = DateTime.Parse(this.TextBoxStartTime.Text).Date;

            CourtSearchSession courtSearch = new CourtSearchSession();
            courtSearch.m_startTime = dt;
            courtSearch.m_cityAreaId = Convert.ToInt32(this.DropDownListCityArea.SelectedValue);
            courtSearch.m_hallId = Convert.ToInt32(this.DropDownListHall.SelectedValue);
            courtSearch.m_timeSpan = Convert.ToDouble(this.TextBoxTimeSpan.Text);

            Session["CourtSearch"] = courtSearch;

            Response.Redirect("~/pages/hallsearchresult.aspx");

        }

        protected void bindHallDropDown(int cityareaid)
        {
            this.DropDownListHall.Items.Clear();
            this.DropDownListHall.Items.Add(new ListItem("不限", "-1"));
            if (cityareaid >= 0)
            {
                BadmintonEntities bde = new BadmintonEntities();
                foreach (Hall ha in bde.Hall.Where(p => p.cityAreaId == cityareaid))
                {
                    this.DropDownListHall.Items.Add(new ListItem(ha.name, ha.id.ToString()));
                }
            }
        }
        protected void OnCityAreaSelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(this.DropDownListCityArea.SelectedValue);
            bindHallDropDown(id);
        }

    }
}